<?php
include_once "../include/config.php";
include "header.php";


if(isset($_GET['place_id'])) {
  $place_id = htmlspecialchars($_GET['place_id']); 
} else if(isset($_POST['place_id'])) {
  $place_id = htmlspecialchars($_POST['place_id']);
} else {
  exit; 
}


// get place info
$place_query = mysql_query("SELECT * FROM places WHERE id='$place_id' LIMIT 1");
if(mysql_num_rows($place_query) != 1) { exit; }
$place = mysql_fetch_assoc($place_query);


function parseInput($value) {
  $value = htmlspecialchars($value, ENT_QUOTES);
  $value = str_replace("\r", "", $value);
  $value = str_replace("\n", "", $value);
  return $value;
}


function datecheck($input,$format="ymd")
    {
        $separator_type= array(
            "/",
            "-",
            "."
        );
        foreach ($separator_type as $separator) {
            $find= stripos($input,$separator);
            if($find<>false){
                $separator_used= $separator;
            }
        }
        $input_array= explode($separator_used,$input);
        if ($format=="mdy") {
            return checkdate($input_array[0],$input_array[1],$input_array[2]);
        } elseif ($format=="ymd") {
            return checkdate($input_array[1],$input_array[2],$input_array[0]);
        } else {
            return checkdate($input_array[1],$input_array[0],$input_array[2]);
        }
        $input_array=array();
    }

// do place edit if requested
if($task == "doedit") {
  $title = parseInput($_POST['title']);
  $type = parseInput($_POST['type']);
  $address = parseInput($_POST['address']);
  $uri = parseInput($_POST['uri']);
  $description = parseInput($_POST['description']);
  $type = parseInput($_POST['type']);
  $start_date = parseInput($_POST['start_date']);
  $end_date = parseInput($_POST['end_date']);
  $hashtag = parseInput($_POST['hashtag']);
  
  mysql_query("UPDATE places SET title='$title', type='$type', address='$address', uri='$uri', lat='', lng='', description='$description', start_date='$start_date', end_date='$end_date', hashtag='$hashtag' WHERE id='$place_id' LIMIT 1") or die(mysql_error());
    
  // geocode
  $hide_geocode_output = true;
  include "../geocode.php";
  
  header("Location: index.php?view=$view&search=$search&p=$p");
  exit;
}

?>



<? echo $admin_head; ?>

<form id="admin" class="form-horizontal" action="edit.php" method="post">
  <h1>
    Lugar de Edición
  </h1>
  <fieldset>
    <div class="control-group">
      <label class="control-label" for="">Nombre Protesta</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="title" value="<?=$place[title]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Tipo Protesta</label>
      <div class="controls">
        <select class="input input-xlarge" name="type">
   		<?php foreach($places_types as $type){ ?>
    		<option<? if($place[type] == $type[0]) {?> selected="selected"<? } ?> value="<?=$type[0]?>"><?=$type[1]?></option>
		<?php } ?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Ubicación</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="address" value="<?=$place[address]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Descripción</label>
      <div class="controls">
        <textarea class="input input-xlarge" name="description"><?=$place[description]?></textarea>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Fecha de Inicio</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="start_date" value="<?=$place[start_date]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Fecha de Fin</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="end_date" value="<?=$place[end_date]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">URL del sitio</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="uri" value="<?=$place[uri]?>" id="">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="">Hashtag</label>
      <div class="controls">
        <input type="text" class="input input-xlarge" name="hashtag" value="<?=$place[hashtag]?>" id="">
      </div>
    </div>
    <div class="form-actions">
    Editar una protesta ya aprobada supone dejarla en estado pendiente para su revisión.<br><br>
      <button type="submit" class="btn btn-primary">Guardar Cambios</button>
      <input type="hidden" name="task" value="doedit" />
      <input type="hidden" name="place_id" value="<?=$place[id]?>" />
      <input type="hidden" name="view" value="<?=$view?>" />
      <input type="hidden" name="search" value="<?=$search?>" />
      <input type="hidden" name="p" value="<?=$p?>" />
      <a href="admin.php" class="btn" style="float: right;">Cancelar</a>
    </div>
  </fieldset>
</form>





<? echo $admin_foot; ?>
